package com.huatai.exchange.integration.execd.impl;

import com.faneui.common.bean.FaneuiBaseResp;
import com.faneui.common.exception.BusinessException;
import com.faneui.integration.http.HttpClientUtils;
import com.google.common.collect.Maps;
import com.huatai.exchange.integration.execd.AbstractExecdService;
import com.huatai.exchange.integration.execd.service.CommandExeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.net.URLEncoder;
import java.util.Map;

@Service
public class CommandExeServiceImpl extends AbstractExecdService implements CommandExeService {

    private static final Logger logger = LoggerFactory.getLogger(CommandExeService.class);

    @Override
    public void exeCommand(String command) throws BusinessException {
        try {
            String url = baseUrl + "/execd";
            Map<String, Object> map = Maps.newHashMap();
            String param = URLEncoder.encode(command, "UTF-8");
            map.put("commandStr", param);
            HttpClientUtils.post(url, map);
        } catch (Exception ex) {
            throw new BusinessException(ex.getMessage());
        }
    }

    @Override
    public String exeCommandAndReturn(String command) throws BusinessException {
        try {
            String url = baseUrl + "/execd";
            Map<String, Object> map = Maps.newHashMap();
            String param = URLEncoder.encode(command, "UTF-8");
            map.put("commandStr", param);
            FaneuiBaseResp resp = HttpClientUtils.postReturn(url, map,FaneuiBaseResp.class);
            return resp.getMsg() ;
        } catch (Exception ex) {
            throw new BusinessException(ex.getMessage());
        }
    }

    @Override
    public void exeBatCommand(String batFilePath) throws BusinessException {
        String command = "cmd.exe " + batFilePath +" \n " ;
        exeCommand(command);
    }

    @Override
    public void copyFile(String filePath, String dirPath) throws BusinessException {
        copyFile(filePath, dirPath, "default");
    }

    @Override
    public void copyFile(String filePath, String dirPath, String ipPath) throws BusinessException {
        String command = "cmd.exe /c copy " + filePath.replace("/", "\\") + " " + dirPath.replace("/", "\\") + " /y";
        if ("default".equals(ipPath)) {
            exeCommand(command);
        }
    }



}
